package com.chw.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.chw.common.ChwTS;
import com.chw.common.FanHui;
import com.chw.mapp.CommerceMapp;

import com.chw.model.Area;
import com.chw.model.Commerce;
import com.chw.model.vo.UserVo;
import com.chw.service.CommerceService;
import com.chw.utils.ChwMD5;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class CommerceServiceImpl implements CommerceService {
    @Autowired
    private CommerceMapp commerceMapp;
    @Autowired
    private HttpServletRequest request;


    //注册新增
    @Override
    public Object addUser(Commerce commerce) {
        commerce.setCreateDate(new Date());
        commerce.setPassword(ChwMD5.chwMD5(ChwMD5.chwMD5(commerce.getPassword())+commerce.getName()));
        Commerce commerce1=commerceMapp.queryName(commerce.getName());
        if (commerce1!=null){
            return 1;
        }
        commerceMapp.addUser(commerce);
        return 0;
    }
 //登录
    @Override
    public Object login(String name, String password) {
        Commerce commerce=commerceMapp.queryName(name);
        if (commerce==null){
            return 1;
        }
        String pass=ChwMD5.chwMD5(ChwMD5.chwMD5(password)+name);
        if (!pass.equals(commerce.getPassword())){
            return 2;
        }
       // request.getSession().setAttribute("login_user",commerce);

        return 0;
    }
    private StringBuffer sb=null;
    @Override
    public String area() {
        List<Area>areas=commerceMapp.area();
        sb=new StringBuffer("[");
        int count=0;
        for (int i=0;i<areas.size();i++){
            Area area=areas.get(i);
            if (area.getPid()!=null&&area.getPid()==0){
                count++;
                sb.append("{");
                sb.append("\"label\":\"").append(area.getName()).append("\",\"value\":").append(area.getId());
                getChildren(area.getId(),areas);
                sb.append("},");
            }
        }
        return sb.substring(0,sb.length()-1)+"]";
    }

    private void getChildren(Integer id, List<Area> areas) {
        sb.append(",\"children\":[");
        int count=0;
        for (int i=0;i<areas.size();i++){
            Area area=areas.get(i);
            if (area.getPid()!=null&&area.getPid().equals(id)) {
                count++;

                sb.append("{");
                sb.append("\"label\":\"").append(area.getName()).append("\",\"value\":").append(area.getId());
                getChildren(area.getId(), areas);
                sb.append("},");
            }
        }
        if (count>0){
            sb.replace(sb.length()-1,sb.length(),"]");
        }else {
            sb.replace(sb.length()-13,sb.length(),"");
        }
    }


    @Override
    public Object queryUser(UserVo userVo) {
        FanHui fanHui=new FanHui();
        Long count=commerceMapp.count(userVo);
        List<Commerce>user=commerceMapp.queryUser(userVo);
        fanHui.setCount(count);
        fanHui.setMsg("success");
        fanHui.setData(user);
        return fanHui;
    }

    @Override
    public Object queryUserById(Integer id) {

        return commerceMapp.queryUserById(id);
    }

    @Override
    public void updateUser(Commerce commerce) {
        commerce.setPassword(ChwMD5.chwMD5(ChwMD5.chwMD5(commerce.getPassword()))+ commerce.getName());
         commerceMapp.updateUser(commerce);
    }
    @Override
    public void updateGeRXinXi(Commerce commerce) {

         commerceMapp.updateGeRXinXi(commerce);
    }

    @Override
    public void deleteUser(Integer[] id) {
        commerceMapp.deleteUser(id);
    }

    @Override
    public Object queryByName(String name) {
        Commerce commerce=commerceMapp.queryName(name);
        if (commerce==null){
            return 1;
        }
        return commerce;
    }

    @Override
    public Object updatePass(String name, String password) {


        String upPs=ChwMD5.chwMD5(ChwMD5.chwMD5(password)+name);
        commerceMapp.updatePass(name,upPs);
        return 0;

    }

    @Override
    public Object updateXinXi(Commerce commerce) {
        Commerce comm=commerceMapp.queryName(commerce.getName());
        if (commerce==null){
            return 1;
        }
        commerceMapp.updateXinXi(commerce);
        return 0;
    }
}
